package cn.edu.thu.tsquality.core.algorithm.variance.constraints;


import cn.edu.thu.tsquality.adaptor.single.SingleAdaptor;
import cn.edu.thu.tsquality.core.common.algorithm.IAlgorithm;
import cn.edu.thu.tsquality.core.common.util.ArgumentsHelper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class SingleMain
{
    public static void main(String[] args) throws IOException
    {

        System.out.println("VarianceConstriants");
        System.out.println("args length="+args.length);
        for(int i = 0; i < args.length; i++)
            System.out.println("arg[" + i + "] = " + args[i]);

        String inputPath = args[0];
        String outputPath = args[1];


        List<String> usedAttrs;
        String attr;
        Integer windowSize;
        Double varianceConstraint;
        boolean isDifference;
        Integer confidence;

        try
        {
            String[] arguments = ArgumentsHelper.getArguments(args[2]);

            if(arguments.length != 2)
                throw new IllegalArgumentException("argument error, expected 2 arguments, got "+args.length);

            String[] attrs = ArgumentsHelper.getValues(arguments[0]);
            if(attrs.length != 1)
                throw new IllegalArgumentException("attrs error, expected 1 attr, got "+attrs.length);

            attr = attrs[0];

            usedAttrs = new ArrayList<>(Arrays.asList(attrs));

            String[] algorithmArguments = ArgumentsHelper.getValues(arguments[1]);


            // 如果3个参数，第三个表示直接指定窗口方差约束值
            // 如果4个参数，第三个参数不使用，第四个参数表示使用相邻变化的最大值计算窗口方差约束值
            if(algorithmArguments.length == 3)
            {
                windowSize = Integer.parseInt(algorithmArguments[0]);
                confidence = Integer.parseInt(algorithmArguments[1]);
                varianceConstraint = Double.parseDouble(algorithmArguments[2]);
                isDifference = false;
            }
            else if(algorithmArguments.length == 4)
            {
                windowSize = Integer.parseInt(algorithmArguments[0]);
                confidence = Integer.parseInt(algorithmArguments[1]);
                varianceConstraint = Double.parseDouble(algorithmArguments[3]);
                isDifference = true;
            }
            else
            {
                throw new IllegalArgumentException("windowSize/varianceConstraint error, expected 3/4 numbers, got " + algorithmArguments.length);
            }

            IAlgorithm algorithm = new VarianceConstraints(attr, windowSize, confidence, varianceConstraint, isDifference);
            SingleAdaptor adaptor = new SingleAdaptor();
            adaptor.setAlgorithm(algorithm);
            adaptor.run(inputPath, outputPath, usedAttrs);


        }
        catch (Exception e)
        {
            System.out.println("Catch Exception: "+e.toString());
            throw e;
        }


    }
}
